"""
锁是最常用的线程同步机制之一，用于保护共享资源，确保同一时间只有一个线程可以访问该资源。

"""

import threading

# 共享资源
counter = 0

# 创建锁对象
lock = threading.Lock()


def increment():
    global counter
    for _ in range(100000):
        # 获取锁
        lock.acquire()
        try:
            counter += 1
        finally:
            # 释放锁
            lock.release()


# 创建俩个线程
t1 = threading.Thread(target=increment)
t2 = threading.Thread(target=increment)

t1.start()
t2.start()

t1.join()
t2.join()

print(f'counter:{counter}')
